home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / lib / c / gate / Gate_Next.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-06-05  |  4.4 KB  |  202 lines

  1. /* 
  2.  * Gate_Next.c --
  3.  *
  4.  *    Source code for the Gate_Next library procedure.
  5.  *
  6.  * Copyright 1992 Regents of the University of California
  7.  * Permission to use, copy, modify, and distribute this
  8.  * software and its documentation for any purpose and without
  9.  * fee is hereby granted, provided that the above copyright
  10.  * notice appear in all copies.  The University of California
  11.  * makes no representations about the suitability of this
  12.  * software for any purpose.  It is provided "as is" without
  13.  * express or implied warranty.
  14.  */
  15.  
  16. #ifndef lint
  17. static char rcsid[] = "$Header: /sprite/src/lib/c/gate/RCS/Gate_Next.c,v 1.1 92/06/04 22:03:22 jhh Exp Locker: jhh $ SPRITE (Berkeley)";
  18. #endif not lint
  19.  
  20. #include <stdio.h>
  21. #include <sprite.h>
  22. #include <ctype.h>
  23. #include <gate.h>
  24. #include <gateInt.h>
  25. #include <stdlib.h>
  26. #include <string.h>
  27. #include <arpa/inet.h>
  28. #include <netinet/in.h>
  29.  
  30. static Net_EtherAddress emptyEtherAddr;
  31. static Net_FDDIAddress  emptyFDDIAddr;
  32.  
  33.  
  34. /*
  35.  *-----------------------------------------------------------------------
  36.  *
  37.  * Gate_Next --
  38.  *
  39.  *    Read the next line from the gateway file and break it into the
  40.  *    appropriate fields of the structure.
  41.  *
  42.  * Results:
  43.  *    The return value is a pointer to a Gate_Entry structure
  44.  *    containing the information from the next line of the file.
  45.  *    This is a statically-allocated structure, which will only
  46.  *    retain its value up to the next call to this procedure.
  47.  *    If the end of the file is reached, or an error occurs, NULL
  48.  *    is returned.
  49.  *
  50.  * Side Effects:
  51.  *    The position in the file advances.
  52.  *
  53.  *-----------------------------------------------------------------------
  54.  */
  55. Gate_Entry *
  56. Gate_Next()
  57. {
  58. #define BUFFER_SIZE 512
  59. #define MAX_NAMES 20
  60.     static Gate_Entry    entry;
  61.     static char          inputBuf[BUFFER_SIZE];
  62.     static char *    fields[MAX_NAMES+2];
  63.     struct in_addr      inetAddr;
  64.     Net_AddressType     currentType;
  65.     register char *    p;
  66.     int            numFields;
  67.     int              c;
  68.     register int        result;
  69.  
  70.     if (gateFile == (FILE *) NULL) {
  71.     return ((Gate_Entry *) NULL);
  72.     }
  73.     /*
  74.      * Loop until a valid entry has been found, or the end of the file
  75.      * has been reached.
  76.      */
  77.     while (!feof(gateFile)) {
  78.     /*
  79.      * First skip any comment lines or blank lines.
  80.      */
  81.  
  82.     while (1) {
  83.         c = getc(gateFile);
  84.         if (c != '#' && c != '\n') {
  85.         break;
  86.         }
  87.         while ((c != '\n') && (c != EOF)) {
  88.         c = getc(gateFile);
  89.         } 
  90.     }
  91.     if (c == EOF) {
  92.         break; 
  93.     }
  94.     ungetc(c, gateFile);
  95.  
  96.     /*
  97.      * Get the gateway line.
  98.      */
  99.  
  100.     if (fgets(inputBuf, BUFFER_SIZE, gateFile) == NULL) {
  101.         continue; 
  102.     }
  103.  
  104.     /*
  105.      * If the line didn't all fit in the buffer, throw away the
  106.      * remainder.
  107.      */
  108.  
  109.     for (p = inputBuf; *p !=0; p++) {
  110.         /* Null loop body */
  111.     }
  112.     if (p[-1] != '\n') {
  113.         do {
  114.         c = getc(gateFile);
  115.         } while ((c != '\n') && (c != EOF));
  116.     }
  117.     if (c == EOF) {
  118.         break; 
  119.     }
  120.     /*
  121.      * Break the line up into fields.
  122.      */
  123.     for (p = inputBuf, numFields = 0; *p != 0; numFields++) {
  124.         fields[numFields] = p;
  125.         while (!isspace(*p)) {
  126.         p++;
  127.         }
  128.         *p = 0;
  129.         p++;
  130.         while (isspace(*p)) {
  131.         p++;
  132.         }
  133.         if (numFields == MAX_NAMES+1) {
  134.         break;
  135.         }
  136.     }
  137.     if (numFields < 4) {
  138.         continue;
  139.     }
  140.  
  141.     /*
  142.      * Fill in gateway description.
  143.      */
  144.  
  145.     entry.desc = fields[0];
  146.  
  147.     /*
  148.      * Determine the network type and parse the network address.
  149.      */
  150.  
  151.     if (!strcmp(fields[1], "ether")) {
  152.         if (strcmp(fields[1], "ether") == 0) {
  153.         currentType = NET_ADDRESS_ETHER;
  154.         } else if (strcmp(fields[1], "ultra") == 0) {
  155.         currentType = NET_ADDRESS_ULTRA;
  156.         } else if (strcmp(fields[1], "fddi") == 0) {
  157.         currentType = NET_ADDRESS_FDDI;
  158.         } else {
  159.         continue;
  160.         }
  161.     }
  162.     entry.netAddr.type = currentType;
  163.     result = Net_StringToAddr(fields[2], currentType, &entry.netAddr);
  164.     entry.netAddr.type = currentType;
  165.     if (result != SUCCESS) {
  166.         continue;
  167.     }
  168.     /*
  169.      * If the address is invalid, then the entry is invalid.
  170.      */
  171.     switch (currentType) {
  172.     case NET_ADDRESS_ETHER:
  173.         if (!Net_EtherAddrCmp(emptyEtherAddr, 
  174.                   entry.netAddr.address.ether)) {
  175.         continue;
  176.         }
  177.     case NET_ADDRESS_FDDI:
  178.         if (!Net_FDDIAddrCmp(emptyFDDIAddr,
  179.                  entry.netAddr.address.fddi)) {
  180.         continue;
  181.         }
  182.     case NET_ADDRESS_ULTRA:
  183.     default:
  184.         break;
  185.     }
  186.     /*
  187.      * Fill in the internet address.
  188.      */
  189.     if (fields[3][0] == '*') {
  190.         /*
  191.          * Empty internet address.
  192.          */
  193.         entry.inetAddr = 0;
  194.     } else {
  195.         entry.inetAddr = Net_StringToInetAddr(fields[3]);
  196.     }
  197.     return &entry;
  198.     }
  199.     return (Gate_Entry *)NULL;
  200. }
  201.  
  202.